package ru.rchervot.sanasar.client.event;

import ru.rchervot.sanasar.client.place.Place;

import com.google.gwt.event.shared.GwtEvent;

public class EventPlaceChange<T extends Place> extends GwtEvent<HandlerPlaceChange<T>> {
	private static Type<HandlerPlaceChange<?>> TYPE = new Type<HandlerPlaceChange<?>>();

	public EventPlaceChange(T place) {
		this.place = place;
	}
	
	public EventPlaceChange() {
	}
	
	private T place;

	@Override
	protected void dispatch(HandlerPlaceChange<T> handler) {
		handler.onPlaceChange(this);
	}

	/**
	 * Gets the type associated with this event.
	 * 
	 * @return returns the handler type
	 */
	public static Type<HandlerPlaceChange<?>> getType() {
		if (TYPE == null) {
			TYPE = new Type<HandlerPlaceChange<?>>();
		}
		return TYPE;
	}


	// The instance knows its PlaceChangeEventHandler is of type T, but the TYPE
	// field itself does not, so we have to do an unsafe cast here.
	@SuppressWarnings("unchecked")
	@Override
	public Type<HandlerPlaceChange<T>> getAssociatedType() {
		return (Type) TYPE;
	}

	public T getPlace() {
		return place;
	}
	
	public static <T extends Place> EventPlaceChange<T> build(T t) {
		return new EventPlaceChange<T>(t);
	}

}
